using System.Collections.Generic;
using Naptime.Dlc;
using UnityEngine;
using UnityEngine.Rendering;
using Voodoo.Pattern;

public class MapCreatorEditor : MonoBehaviour
{
	[SerializeField]
	private Map _map;

	[SerializeField]
	private LevelPreset[] _levelPresets;

	[SerializeField]
	private Mesh _simpleMesh;

	[SerializeField]
	private SkinnedMeshRenderer _simpleMeshRen;

	[SerializeField]
	private Transform _parent;

	[Header("HouseFirst ab levels")]
	[SerializeField]
	private LevelPreset[] _levelPresetsAB;

	[Header("startPos ab")]
	[SerializeField]
	private LevelPreset[] _levelPresetsStartPosAB;

	[Header("RailsDigging ab")]
	[SerializeField]
	private LevelPreset[] _levelPresetsRailsAB;

	[Header("Bonus level")]
	[SerializeField]
	private LevelPreset _bonuslevelPreset;

	[Header("Bonus level")]
	[SerializeField]
	private LevelPreset _minecraftlevelPreset;

	[Header("Draw My level")]
	[SerializeField]
	private LevelPreset _DrawMyLevelPreset;

	private List<Vector2> trashHolder = new List<Vector2>();

	private float X = 0.00815f;

	private float Y = 0.025f;

	private int _level;

	private Texture2D _currentSprite;

	private List<Vector3> blocksPoses = new List<Vector3>();

	public LevelPosType levelPosType;

	public LevelPreset CurrentLevel;

	public int thisLevelIndex;

	private Vector2Int m_GeneratedLevelSize = new Vector2Int(80, 80);

	public Map Map => _map;

	public bool IsBonusLevel { get; private set; }

	private void Start()
	{
		for (int i = 0; i < _parent.childCount; i++)
		{
			blocksPoses.Add(_parent.GetChild(i).localPosition);
		}
		GetCurrentLevel(SingletonMB<GameManager>.Instance.LastLevelIndex);
		_simpleMeshRen.shadowCastingMode = ShadowCastingMode.Off;
		_simpleMeshRen.receiveShadows = false;
	}

	public void GenerateSkeleton()
	{
		blocksPoses.Clear();
		for (int i = 0; i < _parent.childCount; i++)
		{
			blocksPoses.Add(_parent.GetChild(i).localPosition);
		}
	}

	private void Update()
	{
	}

	private bool IsDrawing()
	{
		return SingletonMB<GameManager>.Instance.IsDrawingLevel;
	}

	public void GetCurrentLevel(int lastLevelIndex)
	{
		_level = NintendoPlayerPrefs.GetInt("Level", 0);
		if (_level == 0)
		{
			LevelChosen(0, _levelPresets[_level % _levelPresets.Length]);
			return;
		}
		int num = lastLevelIndex + 1;
		if (_levelPresets[num % _levelPresets.Length].isPartOfDLC)
		{
			// if (Singleton<DLCManager>.Instance.IsLevelUnlocked(_levelPresets[num % _levelPresets.Length]))
			// {
			// 	LevelChosen(num, _levelPresets[num % _levelPresets.Length]);
			// 	CurrentLevel = _levelPresets[num % _levelPresets.Length];
			// 	SingletonMB<GameManager>.Instance.LastLevelIndex = num - 1;
			// 	GenerateLevel();
			// }
			// else
			{
				GetCurrentLevel(num);
			}
		}
		else
		{
			LevelChosen(num, _levelPresets[num % _levelPresets.Length]);
		}
	}

	private void LevelChosen(int lvlInd, LevelPreset lvlPreset)
	{
		CurrentLevel = lvlPreset;
		thisLevelIndex = lvlInd;
		GenerateLevel();
	}

	public void ResetCubes()
	{
		for (int i = 0; i < _parent.childCount; i++)
		{
			Transform child = _parent.GetChild(i);
			child.tag = "Untagged";
			child.gameObject.SetActive(value: false);
		}
	}

	public void GenerateLevel()
	{
		base.transform.localScale = new Vector3(-1f, 1f, 1f);
		for (int i = 0; i < _parent.childCount; i++)
		{
			Transform child = _parent.GetChild(i);
			child.localPosition = blocksPoses[i];
			child.tag = "Finish";
			child.gameObject.SetActive(value: true);
			child.transform.localEulerAngles = new Vector3(Random.Range(0, 12), Random.Range(0, 12), Random.Range(0, 12));
			if (child.TryGetComponent<Rigidbody>(out var component))
			{
				component.isKinematic = true;
			}
		}
		_level = NintendoPlayerPrefs.GetInt("Level", 0);
		if (SingletonMB<DuelModeManager>.Instance != null)
		{
			_level = 0;
		}
		if (SingletonMB<BonusLevelManager>.Instance.IsBonusLevelActive)
		{
			IsBonusLevel = true;
			_simpleMeshRen.material = _bonuslevelPreset.Material;
			_currentSprite = _bonuslevelPreset.Sprite;
			CurrentLevel = _bonuslevelPreset;
			base.transform.position = _bonuslevelPreset.LevelPosition;
			levelPosType = _bonuslevelPreset.LevelPosType;
		}
		else
		{
			IsBonusLevel = false;
			if (Preloader.StartingPositionConfig.IsEnabled && !SingletonMB<DuelModeManager>.Instance)
			{
				Debug.Log(1);
				if (Preloader.OptimizationConfig.IsEnabled)
				{
					_simpleMeshRen.material = _levelPresetsStartPosAB[_level % _levelPresetsStartPosAB.Length].OptimizedMaterial;
					Debug.Log(11);
				}
				else
				{
					_simpleMeshRen.material = _levelPresetsStartPosAB[_level % _levelPresetsStartPosAB.Length].Material;
				}
				_currentSprite = _levelPresetsStartPosAB[_level % _levelPresetsStartPosAB.Length].Sprite;
				CurrentLevel = _levelPresetsStartPosAB[_level % _levelPresetsStartPosAB.Length];
				base.transform.position = _levelPresetsStartPosAB[_level % _levelPresetsStartPosAB.Length].LevelPosition;
				levelPosType = _levelPresetsStartPosAB[_level % _levelPresetsStartPosAB.Length].LevelPosType;
			}
			else if (Preloader.RailDigging.IsEnabled && !SingletonMB<DuelModeManager>.Instance)
			{
				if (Preloader.OptimizationConfig.IsEnabled)
				{
					_simpleMeshRen.material = _levelPresetsRailsAB[_level % _levelPresetsRailsAB.Length].OptimizedMaterial;
				}
				else
				{
					_simpleMeshRen.material = _levelPresetsRailsAB[_level % _levelPresetsRailsAB.Length].Material;
				}
				_currentSprite = _levelPresetsRailsAB[_level % _levelPresetsRailsAB.Length].Sprite;
				CurrentLevel = _levelPresetsRailsAB[_level % _levelPresetsRailsAB.Length];
				base.transform.position = _levelPresetsRailsAB[_level % _levelPresetsRailsAB.Length].LevelPosition;
				levelPosType = _levelPresetsRailsAB[_level % _levelPresetsRailsAB.Length].LevelPosType;
			}
			else if (Preloader.HouseLevelConfig.IsEnabled)
			{
				if (Preloader.OptimizationConfig.IsEnabled)
				{
					_simpleMeshRen.material = _levelPresetsAB[_level % _levelPresetsAB.Length].OptimizedMaterial;
				}
				else
				{
					_simpleMeshRen.material = _levelPresetsAB[_level % _levelPresetsAB.Length].Material;
				}
				_currentSprite = _levelPresetsAB[_level % _levelPresetsAB.Length].Sprite;
				CurrentLevel = _levelPresetsAB[_level % _levelPresetsAB.Length];
				base.transform.position = _levelPresetsAB[_level % _levelPresetsAB.Length].LevelPosition;
				levelPosType = _levelPresetsAB[_level % _levelPresetsAB.Length].LevelPosType;
			}
			else
			{
				_simpleMeshRen.material = CurrentLevel.OptimizedMaterial;
				_currentSprite = CurrentLevel.Sprite;
				base.transform.position = CurrentLevel.LevelPosition;
				levelPosType = CurrentLevel.LevelPosType;
			}
			if (_level == 0 && SingletonMB<DuelModeManager>.Instance == null && Preloader.MinecraftLevelConfig.IsEnabled)
			{
				if (Preloader.OptimizationConfig.IsEnabled)
				{
					_simpleMeshRen.material = _minecraftlevelPreset.OptimizedMaterial;
				}
				else
				{
					_simpleMeshRen.material = _minecraftlevelPreset.Material;
				}
				_currentSprite = _minecraftlevelPreset.Sprite;
				CurrentLevel = _minecraftlevelPreset;
				base.transform.position = _minecraftlevelPreset.LevelPosition;
				levelPosType = _minecraftlevelPreset.LevelPosType;
			}
		}
		List<Vector2> list = new List<Vector2>();
		trashHolder.Clear();
		_simpleMesh.SetUVs(0, list);
		Debug.Log("SIZE: " + m_GeneratedLevelSize.y + " " + m_GeneratedLevelSize.x);
		for (int j = 0; j < m_GeneratedLevelSize.y; j++)
		{
			for (int k = 0; k < m_GeneratedLevelSize.x; k++)
			{
				if (_currentSprite.GetPixel(k * 10 + 5, j * 10 + 5).a == 0f)
				{
					trashHolder.Add(new Vector2(k, j));
					for (int l = 0; l < 24; l++)
					{
						list.Add(new Vector2(0f, 1f));
					}
				}
				else
				{
					Vector2 item = new Vector2(X * (float)k + 0.006f, Y * (float)j + 0.006f);
					for (int m = 0; m < 24; m++)
					{
						list.Add(item);
					}
				}
			}
		}
		Debug.Log("TRASH HOLDER COUNT " + trashHolder.Count);
		Debug.Log("ACTIVE PIXELS " + (m_GeneratedLevelSize.x * m_GeneratedLevelSize.y - trashHolder.Count));
		for (int n = 0; n < trashHolder.Count; n++)
		{
			Transform child2 = _parent.GetChild((int)trashHolder[n].x + (int)trashHolder[n].y * m_GeneratedLevelSize.x);
			child2.localPosition = new Vector3(0f, 0f, -1f);
			child2.tag = "Untagged";
			child2.gameObject.SetActive(value: false);
		}
		_simpleMesh.SetUVs(0, list);
		_map.Processs();
		if (SingletonMB<BonusLevelManager>.Instance.IsBonusLevelActive)
		{
			Object.FindObjectOfType<Progress>().SetMaxCount(m_GeneratedLevelSize.x * m_GeneratedLevelSize.y - trashHolder.Count);
		}
		Object.FindObjectOfType<LevelOrientation>()?.DetermineLevelPosType(levelPosType);
	}
}
